﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using ArunimaErp.Domain;
using ArunimaErp.Models;
using ArunimaErp.ViewModel;

namespace ArunimaErp.Controllers
{
    public class PoStyleController : Controller
    {
        private arunimaEntities db = new arunimaEntities();

        UserAccountRepository userAccountRepository = new UserAccountRepository();

        #region Index
        //
        // GET: /postyle/

        [Authorize]
        public ViewResult Index()
        {
            //int AccountId = userAccountRepository.GetAccouctId(User.Identity.Name);
            //var poStyle = db.postyle.Where(x => x.styleinfo.AccountId == AccountId).ToList();

            var poStyle = db.postyle.ToList();

            return View(poStyle);
        }

        #endregion

        #region Create
        //
        // GET: /postyle/Create
        [Authorize]
        public ActionResult Create()
        {
            int AccountId = userAccountRepository.GetAccouctId(User.Identity.Name);
            var styleInfo = db.styleinfo.Where(s => s.AccountId == AccountId).OrderBy(x => x.StyleNo).ToList();
            var jobInfo = db.jobinfo.ToList();

            ViewBag.StyleId = new SelectList(styleInfo, "StyleId", "StyleNo");
            ViewBag.JobId = new SelectList(jobInfo, "JobInfoId", "JobNo");
            ViewBag.SeasonId = new SelectList(db.season, "SeasonId", "SeasonName");
            ViewBag.FactoryId = new SelectList(db.factory, "FactoryId", "FactoryName");

            return View();
        }

        //
        // POST: /postyle/Create

        [HttpPost]
        [Authorize]
        public JsonResult Create(postyle postyle)
        {
            if (ModelState.IsValid)
            {
                var check = db.postyle.Where(x => x.PoNo == postyle.PoNo).ToList();

                if (check.Count == 0)
                {
                    postyle.CurrentStatus = 1;
                    db.postyle.Add(postyle);
                    db.SaveChanges();

                    int poStyleId = postyle.PoStyleId;
                    DateTime exitDate = postyle.ExitDate;

                    MpsRepository mpsRepository = new MpsRepository();

                    mpsRepository.CreateMps(poStyleId, exitDate);

                    return Json(JsonResponseFactory.SuccessResponse(postyle), JsonRequestBehavior.DenyGet);
                }
                else
                {
                    ModelState.AddModelError("PoNo", "Already Exists");
                }
            }

            var errorList = (from item in ModelState.Values
                             from error in item.Errors
                             select error.ErrorMessage).ToList();

            return Json(JsonResponseFactory.ErrorResponse(errorList), JsonRequestBehavior.DenyGet); 
        }

        #endregion

        #region Copy
        //
        // GET: /postyle/Edit/5

        [Authorize]
        public ActionResult Copy(int id)
        {
            postyle postyle = db.postyle.Single(p => p.PoStyleId == id);

            int AccountId = userAccountRepository.GetAccouctId(User.Identity.Name);
            var styleinfo = db.styleinfo.Where(s => s.AccountId == AccountId).ToList();
            var jobInfo = db.jobinfo.ToList();

            ViewBag.JobId = new SelectList(jobInfo, "JobInfoId", "JobNo", postyle.JobId);
            ViewBag.StyleId = new SelectList(styleinfo, "StyleId", "StyleNo", postyle.StyleId);
            ViewBag.SeasonId = new SelectList(db.season, "SeasonId", "SeasonName", postyle.SeasonId);
            ViewBag.FactoryId = new SelectList(db.factory, "FactoryId", "FactoryName", postyle.FactoryId);

            return View("Create",postyle);
        }

        #endregion

        #region Split

        //
        // GET: /postyle/Edit/5

        [Authorize]
        public ActionResult Split(int id)
        {
            postyle postyle = db.postyle.SingleOrDefault(x => x.PoStyleId == id);

            PoSplitViewModel poSplitViewModel = new PoSplitViewModel
            {
                PoStyleId = postyle.PoStyleId,
                PoNo = postyle.PoNo,
                Fob = postyle.Fob,
                OrderQuantity = postyle.OrderQuantity,
                ExitDate = postyle.ExitDate
            };

            List<PoSplitViewModel> poSplitViewModelList = new List<PoSplitViewModel>();


            poSplitViewModelList.Add(poSplitViewModel);

            return View(poSplitViewModelList);
        }

        //
        // POST: /postyle/Edit/5

        [Authorize]
        [HttpPost]
        public ActionResult Split(IEnumerable<PoSplitViewModel> poSplitViewModel)
        {
            return Content("");
        }

        #endregion

        #region Edit
        //
        // GET: /postyle/Edit/5

        [Authorize]
        public ActionResult Edit(int id)
        {
            postyle postyle = db.postyle.Single(p => p.PoStyleId == id);

            int AccountId = userAccountRepository.GetAccouctId(User.Identity.Name);
            var styleInfo = db.styleinfo.Where(s => s.AccountId == AccountId).ToList();
            var jobInfo = db.jobinfo.ToList();

            ViewBag.JobId = new SelectList(jobInfo, "JobInfoId", "JobNo", postyle.JobId);
            ViewBag.StyleId = new SelectList(styleInfo, "StyleId", "StyleNo", postyle.StyleId);
            ViewBag.SeasonId = new SelectList(db.season, "SeasonId", "SeasonName", postyle.SeasonId);
            ViewBag.FactoryId = new SelectList(db.factory, "FactoryId", "FactoryName", postyle.FactoryId);

            return View(postyle);
        }

        //
        // POST: /postyle/Edit/5

        [HttpPost]
        [Authorize]
        public JsonResult Edit(postyle postyle)
        {
            if (ModelState.IsValid)
            {
                var check = db.postyle.Where(x => x.PoNo == postyle.PoNo && x.PoStyleId != postyle.PoStyleId).ToList();

                if (check.Count == 0)
                {
                    db.Entry(postyle).State = EntityState.Modified;
                    db.SaveChanges();
                    return Json(JsonResponseFactory.SuccessResponse(postyle), JsonRequestBehavior.DenyGet);
                }
                else
                {
                    ModelState.AddModelError("StyleNo", "Already Exists");
                }
            }

            var errorList = (from item in ModelState.Values
                             from error in item.Errors
                             select error.ErrorMessage).ToList();

            return Json(JsonResponseFactory.ErrorResponse(errorList), JsonRequestBehavior.DenyGet); 
        }

        #endregion

        #region Delete
        //
        // GET: /postyle/Delete/5

        [Authorize]
        public ActionResult Delete(int id)
        {
            postyle postyle = db.postyle.Single(p => p.PoStyleId == id);
            return View(postyle);
        }

        //
        // POST: /postyle/Delete/5

        [HttpPost, ActionName("Delete")]
        [Authorize]
        public ActionResult DeleteConfirmed(int id)
        {
            postyle postyle = db.postyle.Single(p => p.PoStyleId == id);

            db.postyle.Remove(postyle);
            db.SaveChanges();

            return Json(JsonResponseFactory.SuccessResponse(postyle), JsonRequestBehavior.DenyGet);
        }

        #endregion

        #region CostSheet

        public ActionResult CostSheet(int id)
        {
            var costSheet = db.costsheet.Where(x => x.PoStyleId == id).ToList();

            if (costSheet.Count != 0)
            {
                return RedirectToAction("Edit", "CostSheet", new { id = id });
            }

            return RedirectToAction("Create", "CostSheet", new { id = id });
        }

        #endregion
    }
}